/**
 */

package com.bei_en.common.util.baidu_tuisong;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.push.auth.PushKeyPair;
import com.baidu.yun.push.client.BaiduPushClient;
import com.baidu.yun.push.constants.BaiduPushConstants;
import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;
import com.baidu.yun.push.model.PushBatchUniMsgRequest;
import com.baidu.yun.push.model.PushBatchUniMsgResponse;
import com.baidu.yun.push.model.PushMsgToSingleDeviceRequest;
import com.baidu.yun.push.model.PushMsgToSingleDeviceResponse;
import com.google.gson.Gson;

/**
 * @author Guocg
 *
 */
public class PushUtil {

    private static Logger log = Logger.getLogger(PushUtil.class);

    static final String KEY = "9qkDvBK9Trcmfzj3O1s3Yqeg";
    static final String SECRET = "SxGT3YMBFecttMAzGA5SoGcMxigP8dU0";

    static PushKeyPair pair = new PushKeyPair(KEY, SECRET);
    static BaiduPushClient pushClient = new BaiduPushClient(pair,
	    BaiduPushConstants.CHANNEL_REST_URL);
    static {
	pushClient.setChannelLogHandler(new YunLogHandler() {

	    @Override
	    public void onHandle(YunLogEvent event) {

		log.info(event.getMessage());
	    }
	});
    }
    
    public static void pushSingleAndroid(String DeviceToken ,PushContentAndroid content) throws PushClientException, PushServerException{
		try {
			PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest()
					.addChannelId(DeviceToken)
					.addMessageType(1).// 1：通知,0:透传消息. 默认为0 注：IOS只有通知.
					addMessage(new Gson().toJson(content)).addDeviceType(3);// deviceType
			PushMsgToSingleDeviceResponse response = pushClient
					.pushMsgToSingleDevice(request);
			System.out.println("msgId: " +new Gson().toJson(content) + ",sendTime: "
					+ response.getSendTime());
		} catch (PushClientException e) {
			if (BaiduPushConstants.ERROROPTTYPE) {
				throw e;
			} else {
				e.printStackTrace();
			}
		} catch (PushServerException e) {
			if (BaiduPushConstants.ERROROPTTYPE) {
				throw e;
			} else {
				System.out.println(String.format(
						"requestId: %d, errorCode: %d, errorMessage: %s",
						e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
			}
		}
    	
    }
    

    public static void pushAndroid(String[] channelIds, PushContentAndroid content)
	    throws PushClientException, PushServerException {

	try {
	    PushBatchUniMsgRequest request = new PushBatchUniMsgRequest().addChannelIds(channelIds)
		    .addMessageType(1).addMessage(new Gson().toJson(content)).addDeviceType(3);

	    PushBatchUniMsgResponse response = pushClient.pushBatchUniMsg(request);

	    log.info(String.format("msgId: %s, sendTime: %d", response.getMsgId(),
		    response.getSendTime()));
	} catch (PushClientException e) {
	    if (BaiduPushConstants.ERROROPTTYPE) {
		throw e;
	    } else {
		e.printStackTrace();
	    }
	} catch (PushServerException e) {
	    if (BaiduPushConstants.ERROROPTTYPE) {
		throw e;
	    } else {
		log.error(String.format("requestId: %d, errorCode: %d, errorMessage: %s",
			e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
	    }
	}

    }

    public static void pushIOS(String[] channelIds2, PushContentIOS content2)
	    throws PushClientException, PushServerException {

	try {
	    PushBatchUniMsgRequest request = new PushBatchUniMsgRequest()
		    .addChannelIds(channelIds2).addMessageType(1)
		    .addMessage(new Gson().toJson(content2)).addDeviceType(4);

	    PushBatchUniMsgResponse response = pushClient.pushBatchUniMsg(request);

	    log.info(String.format("msgId: %s, sendTime: %d", response.getMsgId(),
		    response.getSendTime()));
	} catch (PushClientException e) {
	    if (BaiduPushConstants.ERROROPTTYPE) {
		throw e;
	    } else {
		e.printStackTrace();
	    }
	} catch (PushServerException e) {
	    if (BaiduPushConstants.ERROROPTTYPE) {
		throw e;
	    } else {
		log.error(String.format("requestId: %d, errorCode: %d, errorMessage: %s",
			e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
	    }
	}

    }
}
